package com.google.code.main;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

//import com.google.code.constant.T;
import com.google.code.web.HttpEngine;
import com.google.code.web.KVPair;

public class Vote {

	/**
	 * @param args
	 * @throws Exception
	 */
	private static final String url = "http://toupiao1.suda.edu.cn/Default.aspx";

	public static void main(String[] args) {
		String vote4 = "113";
		HttpEngine eng = new HttpEngine();
		String html = null;
		Document doc = null;
		try {
			eng.get(url, null, null);
			html = eng.getResponseHtml();
		} catch (Exception e) {
			System.out.print(1);
			return;
		}

		doc = Jsoup.parse(html);
		ArrayList<KVPair> fields = dumpAllField(doc);
		ArrayList<KVPair> pairs = dumpVotes(doc);

		if (pairs.size() < 5) {
			System.out.print(1);
			return;
		}

		sort(pairs);

		int i = 0;
		for (i = 10; i < 20; i++) {
			fields.add(new KVPair("Repeater1$ctl" + i + "$CheckBox1", "on"));
		}

		fields.add(new KVPair("ImageButton3.x", "11"));
		fields.add(new KVPair("ImageButton3.y", "50"));

		KVPair[] data = new KVPair[fields.size()];
		for (i = 0; i < fields.size(); i++) {
			if (fields.get(i).getKey().contains("HiddenField"))
				if ((int) (Math.random() * 100) > 30)
					fields.get(i).setVal(vote4);
			data[i] = fields.get(i);
		}

		String s = null;
		try {
			eng.post(url, null, data);
			s = eng.getResponseHtml();
		} catch (Exception e) {
			System.out.print(1);
			return;
		}

		if (s.indexOf("\u6295\u7968\u6210\u529F") != -1)
			System.out.print(0);
		else
			System.out.print(1);
	}

	public static String getInputValue(Document doc, String id) {
		Elements eventValidations = doc.select("input#" + id);
		if (eventValidations.size() > 0)
			return eventValidations.get(0).attr(id);
		System.out.println("input#" + id + " not found");
		return "";
	}

	public static ArrayList<KVPair> dumpAllField(Document doc) {
		ArrayList<KVPair> fields = new ArrayList<KVPair>();

		Elements inputs = doc.select("input");
		for (Element input : inputs)
			if (input.hasAttr("value"))
				fields.add(new KVPair(input.attr("name"), input.attr("value")));

		return fields;
	}

	public static ArrayList<KVPair> dumpVotes(Document doc) {
		ArrayList<KVPair> last4 = new ArrayList<KVPair>();

		Elements votes = doc.select("div.li tr[align=left]:eq(0)>td");
		Pattern p = Pattern.compile("ctl([0-9]+)");
		Matcher m = null;
		for (Element vote : votes) {
			Elements spans = vote.select("span");
			if (spans.size() < 1)
				continue;
			String vs = vote.ownText();
			String id = spans.get(0).attr("id");
			m = p.matcher(id);
			if (!m.find())
				continue;
			id = m.group(1);

			last4.add(new KVPair(id, vs));
		}

		return last4;
	}

	public static void sort(ArrayList<KVPair> vLst) {
		if (vLst == null)
			System.out.println("Error: Vote list is null.");

		int cnt = vLst.size();
		for (int i = 0; i < cnt - 1; i++) {
			for (int j = i + 1; j < cnt; j++) {
				KVPair ip = vLst.get(i);
				KVPair jp = vLst.get(j);
				if (Integer.parseInt(ip.getVal()) > Integer.parseInt(jp
						.getVal())) {
					vLst.set(i, jp);
					vLst.set(j, ip);
				}
			}
		}
	}

}
